import React, {FC, useState} from 'react';
import '../assets/css/Login.css'
import {getLogin} from '../apis/login'
import {useNavigate} from 'react-router-dom'
import { Button, Form, Input,message } from 'antd';
const Login: FC=()=> {
    const navigator = useNavigate()
    const [loading,setLoading] = useState(false)
    const onFinish = async (values: any) => {
        setLoading(true)
        const res:any = await getLogin({username:values.username,pass:values.password}).catch(()=>{})
        if(res&&res.errCode===10000){
            sessionStorage.setItem('token',res.data.token)
            sessionStorage.setItem('menu',JSON.stringify(res.data.menu))
            sessionStorage.setItem('username',values.username)
            message.success('登录成功', 10);
            sessionStorage.setItem('pageUrl','/')
            navigator('/')
        }else{
            message.error(res.errMsg, 10);
        }
    };
    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className="login">
            <div className="loginContent">
                <Form
                    className="formInfo"
                    name="basic"
                    initialValues={{ username:'qqq',password:'qqq' }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item className="title">登录</Form.Item>
                    <Form.Item
                        name="username"
                        rules={[{ required: true, message: '请输入账号' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入密码' }]}
                    >
                        <Input type="password" />
                    </Form.Item>

                    <Form.Item>
                        <Button loading={loading} type="primary" className="loginBtn" htmlType="submit">
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    );
}

export default Login;
